var pageii = null;
var grid = null;
$(function() {
	
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "orderId",
			name : "订单编号",
			width :"200px",
		    isSort:true,
		}, {
			colkey : "mPhone",
			name : "买家账号",
		}, {
			colkey : "itemnum",
			name : "商品数量",
			isSort:true,
		}, {
			colkey : "totalAmount",
			name : "订单总金额",
			renderData:function(rowindex,data,rowdata,colkeyn){
				return parseFloat(data).toFixed(2);
			}
		}, {
			colkey : "",
			name : "订单状态",
			width : '90px',
			isSort:true,
			renderData:function(rowindex,data,rowdata,colkeyn){
				if(rowdata.isValid && rowdata.isDelete){				
					if('1' == rowdata.isValid  && rowdata.isDelete == '0'){
						return "已取消";
					}else if('1' == rowdata.isDelete){
						return "已作废";
					}
				}
                if(rowdata.isValid == '0' && (rowdata.payStatus == '1' || rowdata.payStatus == '6') ){
                	if( rowdata.status == '3' && rowdata.isComment == '1' && rowdata.shipStatus == '5'){
                		return "已完成";
                	}else if( ( rowdata.shipStatus =='1' || rowdata.shipStatus =='2') &&  (rowdata.payStatus == '1' || rowdata.payStatus == '6')){
    					return "已发货";
    				}else if(rowdata.shipStatus =='3' ){
    					return "退款";
    				}else{
    					return "已付款";
    				}
				}else if(rowdata.isValid == '0' && rowdata.payStatus == '3'){
					return "未付款"
				}
				
			}
		}/*, {
			colkey : "payStatus",
			name : "支付状态",
			renderData:function(rowindex,data,rowdata,colkeyn){
			 if('2' == data){
				 return "支付失败";
			 }else if('3' == data){
				 return "未支付";
			 }else if('4' == data){
				 return "处理异常";
			 }else if('5' == data){
				 return "非法参数";
			 }else if('6' == data || '1'){
				 return "支付成功";
			 }else if('7' == data){
				 return "已付款至担保方";
			 }else if('8' == data){
				 return "超时";
			 }else if('9' == data){
				 return "准备中";
			 }else {
				 return "支付失败";
			 }
			}
		}*/, {
			colkey : "memberName",
			name : "收货人",
		}/*, {
			colkey : "shipStatus",
			name : "发货状态",
			renderData:function(rowindex,data,rowdata,colkeyn){
				 if('0' == data){
					 return "未发货";
					 return '<button type="button" id = "'+rowdata.orderId + '" class="btn btn-danger marR10" onClick="javascript:ship(this)">发货</button>';
				 }else if('1' == data){
					 return "已发货";
				 }else if('2' == data){
					 return "部分发货";
				 }else if('3' == data){
					 return "部分退货";
				 }else if('4' == data){
					 return "已退货";
				 }else if('5' == data ){
					 return "已签收";
				 }else if('6' == data){
					 return "e贸易订单，已填写运单号，待申报";
				 }else {
					 return "未发货";
				 }
				}
		}*/, {
			colkey : "expressName",
			name : "配送方式"
		}, {
			colkey : "shipMoney",
			name : "配送费用"
		}, {
			colkey : "orderCreatTime",
			name : "订单创建时间",
			renderData:function(rowindex,data,rowdata,colkeyn){
				console.log(new Date(data).format("yyyyMMddhh:mm:ss"));
				if(data){
					//var time=new Date(data).format("yyyyMMddhhmmss");
					var time = CommnUtil.uuid();
					return "<span class='item-content-sub' onmousemove='tips(this)' onmouseout='clotips()' id='order_"+rowdata.orderId+time +"' nr='"+rowdata.orderCreatTime+"'>"+ data + "</span>";		
				}else{
					return "";
				}
		   }
		}/*, {
			colkey : "orderPayTime",
			name : "付款时间",
			renderData:function(rowindex,data,rowdata,colkeyn){
				if(data){
					var time = CommnUtil.uuid();
					return "<span class='item-content-sub' onmousemove='tips(this)' onmouseout='clotips()' id='order_"+rowdata.orderId+time +"' nr='"+rowdata.orderPayTime+"'>"+ data + "</span>";		
				}else{
					return "";
				}}
		}, {
			colkey : "orderShipTime",
			name : "发货时间",
			renderData:function(rowindex,data,rowdata,colkeyn){

				if(data){
					var time = CommnUtil.uuid();
					return "<span class='item-content-sub' onmousemove='tips(this)' onmouseout='clotips()' id='order_"+rowdata.orderId+time +"' nr='"+rowdata.orderShipTime+"'>"+ data + "</span>";		
				}else{
					return "";
				}
			}
		}, {
			colkey : "orderOverTime",
			name : "成交时间",
			renderData:function(rowindex,data,rowdata,colkeyn){
				if(data){
					var time = CommnUtil.uuid();
					return "<span class='item-content-sub' onmousemove='tips(this)' onmouseout='clotips()' id='order_"+rowdata.orderId+time +"' nr='"+rowdata.orderOverTime+"'>"+ data + "</span>";		
				}else{
					return "";
				}
			}
		}*/
		, {
			colkey : "",
			name : "操作",
			renderData:function(rowindex,data,rowdata,colkeyn){
					return "<a href='javascript:void(0)'  onclick='oneOrder"+'("'+rowdata.orderId+'")'+"'  >查看详细</a>";
			}
		}],
		jsonUrl : rootPath + '/order/findByPage',
		checkbox : true,
		checkValue : 'orderId', // 当checkbox为true时，需要设置存放checkbox的值字段 默认存放字段id的值
	});
	$("#search").click("click", function() {// 绑定查询按扭		
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数		
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addAccount").click("click", function() {
		addAccount();
	});
	$("#editOrder").click("click", function() {
		editOrder();
	});
	$("#deleteOrder").click("click", function() {
		delOrder();
	});
	$("#shipOrder").click("click", function() {
		shipOrder();
	});
	$("#shipProducts").click("click", function() {
		shipProducts();
	});
	
});
//发货
function shipOrder() {	
	var cbox = grid.getSelectedCheckbox();
	//alert(cbox);
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	debugger;
	pageii = layer.open({
		title : "订单发货",
		type : 2,
		area : [ "50%", "40%" ],
		content : rootPath + '/order/shipUI?id=' + cbox
	});
}
var tipslayer="";
function tips(o){
	tipslayer = layer.open({
	    tips:[1,'#0FA6D8'], //还可配置颜色
        closeBtn:0,
        type:4,
        content:[$(o).attr("nr"),"#" + $(o).attr("id")]
});
}
function clotips(){
	layer.close(tipslayer);
}

function editOrder() {
	var cbox = grid.getSelectedCheckbox();
	//alert(cbox);
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "编辑",
		type : 2,
		area : [ "50%", "85%" ],
		content : rootPath + '/order/editUI?id=' + cbox
	});
}
function addAccount() {
	pageii = layer.open({
		title : "新增",
		type : 2,
		area : [ "600px", "80%" ],
		content : rootPath + '/user/addUI'
	});
}
function delOrder() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/order/deleteOrder';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}
//订单详细
function oneOrder(orderId) {
	var url = rootPath + "/order/order_details_frame?orderId="+orderId; //订单详情页
	pageii = layer.open({
		title : "订单详细",
		type : 2,
		area : [ "1100px", "80%" ],
		content : url
	});
	//loadPage(url);
}

